package acme.client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Random;

import acme.entity.Brand;
import acme.entity.Department;
import acme.entity.Entity;
import acme.entity.Product;
import acme.ws.JSONServlet;

public class ClientWorker implements Runnable{
	private static Random random = new Random();
	private static double clickThruMax = .09;
	
	private API api = null;
	private String key = null;
	private long interval = -1;
	private boolean isRunning = false;
	
	public ClientWorker(API api, String key, long interval){
		this.api = api;
		this.key = key;
		this.interval = interval;
	}
	
	public long getInterval() {
		return interval;
	}

	public void setInterval(long interval) {
		this.interval = interval;
	}

	public boolean isRunning() {
		return isRunning;
	}

	public void setRunning(boolean isRunning) {
		this.isRunning = isRunning;
	}

	public void stopWorking() {
		this.isRunning = false;
	}
	
	public void makeCalls(){
		String address = Calls.getRandomCall(api) + this.key;
		try {
			URL url = new URL(address);
			Object obj = url.getContent();
			if(API.CatalogAPI.equals(api)){
				if(random.nextDouble() <= clickThruMax){   //let's click thru
					if(obj instanceof InputStream){
						InputStream inputstream = (InputStream)obj;
						String json = getJson(inputstream);
						closeInput(inputstream);
						
						Entity entity = null;
						if(address.contains("product")){
							entity = JSONServlet.getGson().fromJson(json, Product.class);
						}else if(address.contains("brand")){
							entity = JSONServlet.getGson().fromJson(json, Brand.class);
						}else if(address.contains("department")){
							entity = JSONServlet.getGson().fromJson(json, Department.class);
						}
						String vtAddress = entity.getUrl();
						if(url != null){
							url = new URL(vtAddress);
							obj = url.getContent();
							if(obj instanceof InputStream){
								inputstream = (InputStream)obj;
								closeInput(inputstream);
							}
						}
					}
				}
			}
	
		} catch (MalformedURLException e1) {
			e1.printStackTrace();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
	}
	
	@Override
	public void run() {
		isRunning = true;
		
		while(isRunning){
			makeCalls();
			
			try{
				Thread.sleep(interval);
			}catch(Exception e){
				e.printStackTrace();
			}
		}
	}
	
	private void closeInput(InputStream inputstream){
		if(inputstream != null){
			try {
				inputstream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	private static String getJson(InputStream is){
		String json = null;
		try {
			BufferedReader reader = new BufferedReader(new InputStreamReader(is));
			StringBuilder sb = new StringBuilder();
			String line = reader.readLine();
			while(line != null){
				sb.append(line);
				line = reader.readLine();
			}
			json = sb.toString();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return json;
	}
		
	
}
